import Vue from 'vue'
import Router from 'vue-router'
import Landing from './components/Landing.vue'
import Register from './components/Register.vue'
import Login from './components/Login.vue'
import Dashboard from './components/Dashboard.vue'
import CreatedProfile from './components/CreatedProfile.vue'
import EditProfile from './components/EditProfile'
import AddEducation from './components/AddEducation.vue'
import AddExperience from './components/AddExperience.vue'
import Profiles from './components/Profiles.vue'
import Feed from "./components/posts/Posts.vue";
import Post from './components/posts/Post.vue'


Vue.use(Router)

// 设置路由
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  linkActiveClass: 'active',
  routes: [
    { path: '*', redirect: '/' },
    { path: '/', component: Landing },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/dashboard', component: Dashboard },
    { path: '/createdProfile', component: CreatedProfile },
    { path: '/edit-profile', component: EditProfile },
    { path: '/add-education', component: AddEducation },
    { path: '/add-experience', component: AddExperience },
    { path: '/Profiles', component: Profiles },
    { path: '/Feed', component: Feed },
    { path: '/Post/:id', component: Post },




  ]

})


// 路由守卫
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  const islogin = window.localStorage.jwt ? true : false
  if (to.path === '/login' || to.path === '/register' || to.path === '/' || to.path === '/Profiles') {
    next()
  } else {
    // alert('请先登录')
    islogin ? next() : next('/login')
  }
})

export default router
